#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time: 2022/12/27 15:09
# @Author: ysy
# @Email: 928233621@qq.com
# @File：add_pages.py
from appium.webdriver.common.mobileby import MobileBy as By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from appium.webdriver.common.touch_action import TouchAction

class addPages:
    # 添加按钮定位器
    btn_add = (By.ID, "com.gfeng.daydaycook:id/iv_add")
    btn_add_szp = (By.XPATH, "//android.widget.FrameLayout/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]")
    btn_add_xsp = (By.XPATH, "//android.widget.FrameLayout/android.widget.FrameLayout[2]/android.widget.LinearLayout[1]")
    btn_add_cgx = (By.XPATH, "//android.widget.FrameLayout/android.widget.FrameLayout[3]/android.widget.LinearLayout[1]")
    # 选择图片定位器
    choice_img_locator = (By.ID, "com.gfeng.daydaycook:id/mTvIndex")
    # 选择图片中的下一步定位器
    btn_next_locator = (By.ID, "com.gfeng.daydaycook:id/mTvNext")
    # 标题定位器
    text_title_locator = (By.ID, "com.gfeng.daydaycook:id/et_title")
    # 这一刻想说点什么，描述定位器
    text_edittext_locator = (By.ID, "com.gfeng.daydaycook:id/edittext")
    # 发布定位器
    btn_publish_locator = (By.ID, "com.gfeng.daydaycook:id/publish_textview")
    # 发布成功后的提示框中的二维码分享定位器
    toast_locator = (By.ID, "com.gfeng.daydaycook:id/tv_share_click")

    # 写食谱页面定位器
    # 菜谱封面定位器
    choice_cookbook_locator = (By.ID, "com.gfeng.daydaycook:id/rl_cover")
    # 菜谱菜名定位器
    text_dish_locator = (By.ID, "com.gfeng.daydaycook:id/et_recipe_name")
    # 菜的故事
    text_story_locator = (By.ID, "com.gfeng.daydaycook:id/et_recipe_description")
    # 菜谱中下一步进入编辑页面的定位器
    btn_next_sp_locator = (By.ID, "com.gfeng.daydaycook:id/tv_next")

    # 写食谱页面定位器
    # 食材定位器
    text_food_sp_locator = (By.ID, "com.gfeng.daydaycook:id/et_food_name")
    # 用量定位器
    text_num_sp_locator = (By.ID, "com.gfeng.daydaycook:id/et_num_unit")
    # 描述步骤定位器
    text_step_sp_locator = (By.ID, "com.gfeng.daydaycook:id/et_step_description")
    # 食谱分类定位器
    # choice_class_sp_locator = (By.ID, "com.gfeng.daydaycook:id/nameTv")
    choice_class_sp_locator = (By.ID, "com.gfeng.daydaycook:id/tfl_recipe_category")
    # 食谱页面发布按钮定位器
    btn_publish_xsp_locator = (By.ID, "com.gfeng.daydaycook:id/tv_publish")


    # 草稿箱页面定位器
    click_cgx_locator = (By.ID, "com.gfeng.daydaycook:id/tv_draft_info")
    del_cgx_locator = (By.ID, "android:id/button1")

    # 用以判断是食谱页面还是晒作品页面
    page_szp_locator = (By.XPATH, "//android.widget.FrameLayout")
    page_xsp_locator = (By.XPATH, "//android.widget.TextView[@text=\"写食谱\"]")

    def __init__(self, driver):
        self.driver = driver


    # 用以判断草稿为写食谱页面还是晒作品页面
    def judge_page(self):
        flag = True
        try:
            xpath_1 = self.driver.find_element(*self.page_xsp_locator).text
            if xpath_1 == "写食谱":
                flag = False
        except Exception:
            pass
        if flag == True:
            return "作品"
        return "食谱"
    # 点击添加按钮
    def click_add(self):
        self.driver.find_element(*self.btn_add).click()

    # 晒作品按钮
    def click_szp(self):
        self.driver.find_element(*self.btn_add_szp).click()

    # 写食谱按钮
    def click_xsp(self):
        self.driver.find_element(*self.btn_add_xsp).click()

    # 草稿箱按钮
    def click_cgx(self):
        self.driver.find_element(*self.btn_add_cgx).click()

    #选择图片
    def click_img(self):
        self.driver.find_element(*self.choice_img_locator).click()

    #点击下一步,连续点击两次
    def click_next(self):
        self.driver.find_element(*self.btn_next_locator).click()

    #输入晒作品标题
    def input_title(self, value):
        # self.driver.find_element(*self.text_title_locator).click()
        self.driver.find_element(*self.text_title_locator).send_keys(value)

    #输入晒作品内容
    def input_content(self, value):
        self.driver.find_element(*self.text_edittext_locator).click()
        self.driver.find_element(*self.text_edittext_locator).send_keys(value)

    # 作品页面点击发布
    def click_publish(self):
        self.driver.find_element(*self.btn_publish_locator).click()
    # 食谱页面点击发布
    def click_publish_xsp(self):
        self.driver.find_element(*self.btn_publish_xsp_locator).click()

    # 发布成功后等待并捕捉返回结果
    def publish_result(self):
        # 设置等待
        wait = WebDriverWait(self.driver, 10, 0.5)
        # 等待元素被加载
        element = wait.until(EC.presence_of_element_located(self.toast_locator))
        return element

    def publish_fail(self):
        toast_messge = ["*请填写食材","*请完善步骤","*请添加食品分类"]
        wait = WebDriverWait(self.driver, 10, 0.5)
        try:
            for i in toast_messge:
                loc = '//*[contains(@text,"{}")]'.format(i)
                wait.until(EC.presence_of_element_located((By.XPATH, loc)))
                tishi = self.driver.find_element(By.XPATH, loc).text
                if tishi == i:
                    print(tishi)
                    return tishi
        except Exception:
            pass

    # 长按删除草稿箱
    def del_cgx(self):
        action = TouchAction(self.driver) # 实例化TouchAction对象
        el = self.driver.find_element(*self.click_cgx_locator)
        action.long_press(el).perform().wait(2000)
        self.driver.find_element(*self.del_cgx_locator).click()

    # 第一条草稿箱时间
    def first_cgx_time(self):
        del_time_text = self.driver.find_element(*self.click_cgx_locator).text
        return del_time_text

    # 点击进入第一条草稿
    def enter_cgx(self):
        self.driver.find_element(*self.click_cgx_locator).click()

    # 选择菜谱封面图片
    def cookbook_img(self):
        self.driver.find_element(*self.choice_cookbook_locator).click()

    # 填写菜名
    def input_xsp_name(self, value):
        self.driver.find_element(*self.text_dish_locator).send_keys(value)

    # 填写菜名
    def input_xsp_content(self, value):
        self.driver.find_element(*self.text_story_locator).send_keys(value)

    # 点击下一步进入编辑菜谱页面
    def click_next_xsp(self):
        self.driver.find_element(*self.btn_next_sp_locator).click()
    # 填写菜名
    def input_food_xsp(self, name, dose):
        self.driver.find_element(*self.text_food_sp_locator).send_keys(name)
        self.driver.find_element(*self.text_num_sp_locator).send_keys(dose)
    # 输入描述步骤
    def input_step_xsp(self, step):
        self.driver.find_element(*self.text_step_sp_locator).send_keys(step)
    # 选择食谱分类
    def choice_class_xsp(self, value):
        xpath = (By.XPATH, "//android.widget.TextView[@text=\"{}\"]".format(value))
        self.driver.find_element(*xpath).click()
    # 向上滑动屏幕
    def swipe(self):
        self.driver.swipe(500, 1500, 500, 200)